# coding:utf-8
'''
Created on 2014-12-23

@author: scemoon
'''
from django.db.models import Min, Max, Avg, StdDev
from django import template
from lpts.models import Value
import json


register = template.Library()

@register.filter
def getmtm(queryset, name):
    '''get objects that many to many related'''
    return getattr(queryset, name).all()

@register.filter
def key(dic,  key):
    return dic.get(key)
    
@register.filter
def reversequery(obj, obj_set):
    '''过滤obj name 于reverse_name为一对多的关系，反向过滤所有reverse_name的objects
    @param obj:  
    @note: 使用于一对多
    '''
    return  getattr(obj, obj_set).all()

@register.filter
def filter_by_testclass(objs, testclass_obj):
    '''过滤result_objects中scene__testclass=testclass_obj的result_objects'''
    return objs.filter(scene__testclass=testclass_obj)

@register.filter
def filter_by_testcontent(objs, testcontent_obj):
    '''过滤result_objects中scene__testcontent=testcontent_obj的result_objects'''
    return objs.filter(scene__testcontent=testcontent_obj)

@register.filter
def filter_by_scene(objs, scene_obj):
    return objs.filter(scene=scene_obj)

@register.filter
def filter_by_index(objs, index_obj):
    '''过滤index 为index_obj的Value objects'''
    return objs.filter(result__index=index_obj)

@register.filter
def filter_by_parallel(objs, parallel):
    '''过滤并行数为parallel的Value objects'''
    return objs.filter(result__parallel=parallel)

@register.filter
def index(objs):
    return objs.values("index").distinct()

@register.filter
def querysetfilter(queryset,  namedic):
    '''过滤namedic的queryset'''
    return  queryset.filter(**namedic)

@register.filter
def querysetget(queryset,  name):
    '''获取名称为name的queryset'''
    return  queryset.get(**name)

@register.filter
def  objectvalue(instance_obj, name):
    '''获取instance_objs实例中名称为name的value'''
    return getattr(instance_obj, name)

@register.filter
def mmms(instance_object):
    '''获取 queryset的最小值、最大值、平均值和标准方差
    通过job object反向获取value object并求取最小、最大、平均值'''
    return instance_object.value_set.aggregate(Min("value"), Max('value'), Avg('value'))

@register.filter
def order_by(queryset, field):
    '''按filed排序'''
    return queryset.order_by(field)

@register.filter
def mmm(querset):
    '''获取queryset的最大、最小、平均值
    @param queryset: queryset为value objects 集合'''
    return querset.aggregate(Min("value"), Max('value'), Avg('value'))


@register.assignment_tag
def get_data(objs):
    data = {"data":[[obj.parallel, float(obj.value_set.aggregate(Avg("value")).get("value__avg"))] for obj in objs], "name":"当前平均值"}
    history_min_data = {"data":[[   obj.parallel, float(Value.objects.filter(result__index=obj.index, result__parallel=obj.parallel).aggregate(Min("value")).get("value__min")) ] for obj in objs],
                        "name" :"历史最小值"}
    history_max_data = {"data":[[   obj.parallel, float(Value.objects.filter(result__index=obj.index, result__parallel=obj.parallel).aggregate(Max("value")).get("value__max")) ] for obj in objs],
                        "name" :"历史最大值"}
    history_avg_data = {"data":[[   obj.parallel, float(Value.objects.filter(result__index=obj.index, result__parallel=obj.parallel).aggregate(Avg("value")).get("value__avg")) ] for obj in objs],
                        "name" :"历史平均值"}
    #para_data = [ {"data":[["当前平均值", float(obj.value_set.aggregate(Avg("value")).get("value__avg")) ],
                           #["历史平均值", float(Value.objects.filter(result__index=obj.index, result__parallel=obj.parallel).aggregate(Avg("value")).get("value__avg"))],
                           #["历史最大值", float(Value.objects.filter(result__index=obj.index, result__parallel=obj.parallel).aggregate(Max("value")).get("value__max"))],
                           #["历史最小值", float(Value.objects.filter(result__index=obj.index, result__parallel=obj.parallel).aggregate(Min("value")).get("value__min"))] ], "name": obj.parallel}  for obj in objs]
    return [data,history_avg_data, history_min_data, history_max_data]
    #return  para_data               